package dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_tally;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.R;

/**
 * 自定义数字键盘
 */
public class NumPlateView extends LinearLayout implements View.OnClickListener {

    /**
     * 0~9,符号等
     */
    private List<String> numList = new ArrayList<>();
    /**
     * 键盘的背景颜色
     */
    private final int backgroundColor = Color.parseColor("#e9e9e9");
    /**
     * 键盘文字颜色
     */
    private final int keyTextColor = Color.parseColor("#333333");
    /**
     * 键盘列数
     */
    private final int spanCount = 4;
    /**
     * 键盘 键的间隔
     */
    private final int keyButtonMargin = 15;
    /**
     * 键盘上下左右的边距
     */
    private final int keyboardPadding = 10;
    /**
     * 按键点击回调
     */
    private OnKeyClickListener onKeyClickListener;

    private KeyAdapter keyAdapter;
    public NumPlateView(Context context) {
        super(context);
        init(context);
    }

    public NumPlateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        setOrientation(LinearLayout.VERTICAL);
        setBackgroundColor(backgroundColor);
        initKeys();
        RecyclerView recyclerView = new RecyclerView(context);
        recyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
        recyclerView.setLayoutManager(new GridLayoutManager(context, spanCount));
        recyclerView.addItemDecoration(new RecycleGridDivider(keyButtonMargin));
        int padding = dip2px(context, keyboardPadding);
        recyclerView.setPadding(padding, padding, padding, padding);
        addView(recyclerView);

        keyAdapter = new KeyAdapter(this);
        recyclerView.setAdapter(keyAdapter);
        keyAdapter.setNewData(numList);
    }

    /**
     * 初始化按键
     */
    private void initKeys(){
        String[] num = getResources().getStringArray(R.array.nums);
        Collections.addAll(numList,num);
    }

    @Override
    public void onClick(View v) {
        TextView tvKey = v.findViewById(R.id.tv_key);
        String key = tvKey.getText().toString();
        if(onKeyClickListener!=null){
            onKeyClickListener.onKeyClick(key);
        }
    }

    public class KeyAdapter extends RecyclerView.Adapter<KeyAdapter.KeyViewHolder>{

        private List<String> list = new ArrayList<>();
        private View.OnClickListener listener;
        private KeyAdapter(View.OnClickListener listener){
            this.listener = listener;
        }

        @NonNull
        @Override
        public KeyAdapter.KeyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_key,parent,false);
            return new KeyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull KeyAdapter.KeyViewHolder holder, int position) {
            String key = list.get(position);
            holder.tvKey.setText(key);
            holder.itemView.setOnClickListener(listener);
            if(TextUtils.isEmpty(key)){
                holder.itemView.setBackgroundResource(0);
            }
            else{
                holder.tvKey.setTextSize(12);
                holder.itemView.setBackgroundResource(R.drawable.sel_white_radius_2);
                holder.tvKey.setTextColor(keyTextColor);
            }
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public void setNewData(List<String> list){
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }

        private class KeyViewHolder extends RecyclerView.ViewHolder{
            private TextView tvKey;
            public KeyViewHolder(@NonNull View itemView){
                super(itemView);
                tvKey = itemView.findViewById(R.id.tv_key);
            }
        }
    }



    public class RecycleGridDivider extends RecyclerView.ItemDecoration{
        private int space;

        public RecycleGridDivider(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            GridLayoutManager manager = (GridLayoutManager)parent.getLayoutManager();
            int span = manager.getSpanCount();
            int offset = space/2;
            int childPosition = parent.getChildAdapterPosition(view);
            if(childPosition<span){
                //最左边的，左边不画
                if (childPosition % span == 0) {
                    outRect.set(0, 0, offset, 0);
                    //最右边，右边不画
                } else if (childPosition % span == span - 1) {
                    outRect.set(offset, 0, 0, 0);
                } else {
                    outRect.set(offset, 0, offset, 0);
                }
            }
            else{
                if (childPosition % span == 0) {
                    outRect.set(0, space, offset, 0);
                } else if (childPosition % span == span - 1) {
                    outRect.set(offset, space, 0, 0);
                } else {
                    outRect.set(offset, space, offset, 0);
                }
            }

        }
    }
    public void setOnKeyClickListener(OnKeyClickListener listener){
        this.onKeyClickListener = listener;
    }
    public interface OnKeyClickListener{
        void onKeyClick(String key);
    }
    public static int dip2px(Context context,float dpValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dpValue*scale+0.5f);
    }

}
